package com.zoke.loveway;

import android.content.Context;

import com.zoke.loveway.utils.ScreenManager;
import com.zoke.support.base.BaseApp;
import com.zoke.support.file.FStoreLoc;
import com.zoke.support.file.Storage;
import com.zoke.support.file.FStoreLoc.DirLevel;
import com.zoke.support.file.exception.FileCreateFailureException;
import com.zoke.support.file.exception.SdCardNotMountedException;
import com.zoke.support.file.exception.SdCardNotValidException;
import com.zoke.support.http.CallBackStores;

/**
 * 
 * @author Jack
 *
 */
public class LoveWayApp extends BaseApp {

	@Override
	public void onCreate() {
		super.onCreate();
		initFileStores(getApplicationContext());
	}

	@Override
	protected String imageCacheDir() {
		return getImageCacheDir(getApplicationContext());
	}

	@Override
	public void exitApp() {
		CallBackStores.getCallback().clearCallbacks();
		ScreenManager.getScreenManager().popAllActivityExceptOne();
	}

	/** 初始化目录体系 **/
	private void initFileStores(Context context) {
		// 获取缓存目录
		FStoreLoc.BIGFILE.setBaseDirName(context, LoveConfig.APP_DIR_NAME);
		FStoreLoc.BIGFILE.switchTo(context, Storage.CARD_EXT);
	}

	/** 获取缓存目录 cache/images/xxxx cache目录要清理缓存 **/
	public static String getImageCacheDir(Context context) {
		String cacheDir = "";
		try {
			// 所有图片的地址 切换图片的缓存地址 -- 优先使用sd卡
			cacheDir = FStoreLoc.SURVIVE.getImagesCacheDir(context,
					DirLevel.DEFAULT).getPath();
		} catch (SdCardNotMountedException e1) {
			e1.printStackTrace();
		} catch (SdCardNotValidException e1) {
			e1.printStackTrace();
		} catch (FileCreateFailureException e1) {
			e1.printStackTrace();
		}
		return cacheDir;
	}

	/** 获取缓存目录 cache/images/xxxx cache目录要清理缓存 **/
	public static String getCacheDir(Context context) {
		String cacheDir = "";
		try {
			// 所有图片的地址 切换图片的缓存地址 -- 优先使用sd卡
			cacheDir = FStoreLoc.SURVIVE.getCacheDir(context, DirLevel.DEFAULT)
					.getPath();
		} catch (SdCardNotMountedException e1) {
			e1.printStackTrace();
		} catch (SdCardNotValidException e1) {
			e1.printStackTrace();
		} catch (FileCreateFailureException e1) {
			e1.printStackTrace();
		}
		return cacheDir;
	}

}
